#include "mpp_display_planes.h"
#include <iostream>
using namespace std;



mpp_display_planes::mpp_display_planes():
                                            mpp_display_element("/dev/dri/card0"),
                                            // buffer(NULL),
                                            //planes(NULL),
                                            // count_planes(0),
                                            possible_planes(0)
                                            // width(width),
                                            // height(height),
                                            // format(format)
{
    
}
mpp_display_planes::~mpp_display_planes()
{
    printf("销毁各个图层\n");
}

int  mpp_display_planes::mpp_planes_create(int fd ,uint32_t count_crtc)
{
    drmModePlaneRes* plane_res ;
    
    uint32_t i = 0,j = 0;

    planes.clear();
    plane_res = NULL;


    plane_res = drmModeGetPlaneResources(fd);   // --拿到图层资源
    if (!plane_res) {
        printf("failed to get plane resources\n");
         return -1;
    }
   
   
    for (i = 0; i < count_crtc; i++) {    //--拿到图层
        drmModePlanePtr tmp = drmModeGetPlane(fd, plane_res->planes[i]);
        PLANE_INFO tmpInfo ;
        if (!tmp) {
            printf("failed to get plane %d\n", i);
            return -1;
        }
        tmpInfo.planes = tmp;
        tmpInfo.is_use = false;
        planes.push_back(tmpInfo);

        // connector下连接若干encoder，
        //每个encoder支持若干crtc，possible_crtcs的某一位为1代表相应次序（不是id哦）的crtc可用。
        cout<<"图层ID:"<<tmp->plane_id<<" CRTC ID:"<<tmp->crtc_id 
                <<"   对应次序:"<<tmp->possible_crtcs 
                <<"("<<i<<")"<< endl;
        
    }
    if(!planes.empty()){
        cout<<"图层数量:"<<count_crtc<<endl;
        for (j = 0; j < count_crtc; j++) {
            if (planes.at(j).planes->possible_crtcs & (1 << j))
                        this->possible_planes=j;
        }
        cout<<"对应图层数量:"<<  this->possible_planes+1<<endl;
    }
    cout<<"获取所有图层"<<"OK"<<endl;
    return 0;
}
void  mpp_display_planes::mpp_planes_distroy()
{
    uint32_t i = 0;
    if(!planes.empty()){
            for(i = 0;i<planes.size();i++){
                if(planes.at(i).planes){
                    drmModeFreePlane(planes.at(i).planes);
                }
            }
           planes.clear(); 
    }
}
void mpp_display_planes::mpp_planes_put_plane(drmModePlanePtr plane)
{
    drmModeSetPlane(mpp_display_dev_get_fd(),plane->plane_id,plane->crtc_id, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0);
}
drmModePlanePtr mpp_display_planes::mpp_get_planes(uint32_t format)
{   
    for(int i = 0;i<planes.size();i++){
        if(planes.at(i).planes) {
            for(int j = 0;i<planes.at(i).planes->count_formats;j++)
                    if(planes.at(i).planes->formats[j] == format){
                        if(planes.at(i).is_use)
                                continue;
                        if (!(planes.at(i).planes->possible_crtcs & (1 << this->possible_planes  )))
                                 continue;
                        planes.at(i).is_use = true;
                        return planes.at(i).planes;
                    }     
        }
    }
    return NULL;
}
